package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.contractuser.pojo.dto.ContractUserDeleteBatchDTO;
import com.xbongbong.pro.contractuser.pojo.vo.ContractUserDeleteBatchVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.service.toolbox.elasticsearch.help.ProEsHelper;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.ContractUserEntity;
import com.xbongbong.saas.model.ContractUserModel;
import com.xbongbong.saas.service.ContractUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/3/13 16:29
 * @since v1.0
 */
@Service("contractUserService")
public class ContractUserServiceImpl implements ContractUserService {

    private static final Logger LOG = LoggerFactory.getLogger(ContractUserServiceImpl.class);

    @Resource
    private ContractUserModel contractUserModel;
    @Resource
    private ProEsHelper proEsHelper;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public ContractUserDeleteBatchVO deleteBatch(ContractUserDeleteBatchDTO contractUserDeleteBatchDTO) throws XbbException {
        ContractUserDeleteBatchVO contractUserDeleteBatchVO = new ContractUserDeleteBatchVO();
        List<Long> contractIdIn = contractUserDeleteBatchDTO.getContractIdIn();
        String corpid = contractUserDeleteBatchDTO.getCorpid();
        try {
            contractUserModel.deleteByContractIdIn(contractIdIn, corpid);

        } catch (Exception e) {
            updateRollbackByContractId(contractIdIn, corpid);
            LOG.error("ContractUserServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return contractUserDeleteBatchVO;
    }

    @Override
    public void dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) throws XbbException {
        try {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("userId", userId);
            param.put("del", 0);
            List<ContractUserEntity> contractUserEntityList = contractUserModel.findEntitys(param);
            for (ContractUserEntity entity : contractUserEntityList) {
                entity.setUserName(userName);
                entity.setUserAvatar(userAvatar);
                contractUserModel.update(entity);
            }

        } catch (Exception e) {
            LOG.error("ContractUserServiceImpl.dataConsistencyUpdateUser 出错，corpid=" + corpid + " userId=" + userId + "  userName=" + userName + "  userAvatar=" + userAvatar, e);
        }
    }

    @Override
    public void updateRollbackByContractId(List<Long> contractIdIn, String corpid) throws XbbException {
        if (Objects.isNull(contractIdIn) || contractIdIn.isEmpty()) {
            return;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put("contractIdIn", contractIdIn);
        List<ContractUserEntity> performanceEntityList = contractUserModel.findEntitys(param);
        if (Objects.nonNull(performanceEntityList)) {
            List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM, performanceEntityList);
            for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
                proEsHelper.reductionEsInUpdate(paasFormDataEntity, paasFormDataEntity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            }
        }
    }
}
